package com.lry.user.controller;

import com.lry.entity.order.Order;
import com.lry.proxy.RpcCallback;
import com.lry.rpc.netty.RpcContext;
import com.lry.rpc.starter.annotation.RpcReference;
import com.lry.service.order.OrderAsyncService;
import com.lry.service.order.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/user")
public class UserController {

    @RpcReference
    private OrderService orderService;

    @GetMapping("/getOrderById")
    public Order getOrderById(@RequestParam Integer orderId){
        RpcContext.set("username","lry");
        Order order = orderService.getById(orderId);
        System.out.println(order);
        return order;
    }

    @RpcReference(async = true)
    private OrderAsyncService orderAsyncService;

    @GetMapping("/getOrderByIdAsync")
    public Order getOrderByIdAsync(@RequestParam Integer orderId) throws ExecutionException, InterruptedException {
        RpcContext.set("username","lry");
        Future<Order> future = orderAsyncService.getById(orderId, new RpcCallback<Order>() {
            @Override
            public void success(Order order) {
                System.out.println(order);
            }

            @Override
            public void fail(Throwable throwable) {
                throwable.printStackTrace();
            }
        });
        Order order = future.get();
        return order;
    }
}
